package org.esprit.ocm.client.model.oca.datasource;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.model.oca.HostRecord;
import org.esprit.ocm.model.oca.HostOca;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class HostLocalDS extends DataSource {

	private static HostLocalDS	instance	= null;

	public static HostLocalDS getInstance(List<HostOca> lists) {
//		if (instance == null) {
			instance = new HostLocalDS("hostLocalDS", lists);
//		}
		return instance;
	}

	public HostLocalDS(String id, List<HostOca> lists) {

//		setID(id);

		DataSourceTextField idField = new DataSourceTextField("id", "id");
		DataSourceTextField name = new DataSourceTextField("name", "name");
		DataSourceTextField state = new DataSourceTextField("state", "state");
		DataSourceTextField im_mad = new DataSourceTextField("im_mad", "im_mad");
		DataSourceTextField vm_mad = new DataSourceTextField("vm_mad", "vm_mad");
		DataSourceTextField tm_mad = new DataSourceTextField("tm_mad", "tm_mad");
		DataSourceTextField last_mon_time = new DataSourceTextField("last_mon_time", "last_mon_time");
		DataSourceTextField cluster = new DataSourceTextField("cluster", "cluster");

		DataSourceTextField host_share_hid = new DataSourceTextField("host_share_hid", "host_share_hid");
		DataSourceTextField host_share_disk_usage = new DataSourceTextField("host_share_disk_usage", "host_share_disk_usage");
		DataSourceTextField host_share_mem_usage = new DataSourceTextField("host_share_mem_usage", "host_share_mem_usage");
		DataSourceTextField host_share_cpu_usage = new DataSourceTextField("host_share_cpu_usage", "host_share_cpu_usage");
		DataSourceTextField host_share_max_disk = new DataSourceTextField("host_share_max_disk", "host_share_max_disk");
		DataSourceTextField host_share_max_mem = new DataSourceTextField("host_share_max_mem", "host_share_max_mem");
		DataSourceTextField host_share_max_cpu = new DataSourceTextField("host_share_max_cpu", "host_share_max_cpu");
		DataSourceTextField host_share_free_disk = new DataSourceTextField("host_share_free_disk", "host_share_free_disk");
		DataSourceTextField host_share_free_mem = new DataSourceTextField("host_share_free_mem", "host_share_free_mem");
		DataSourceTextField host_share_free_cpu = new DataSourceTextField("host_share_free_cpu", "host_share_free_cpu");
		DataSourceTextField host_share_used_disk = new DataSourceTextField("host_share_used_disk", "host_share_used_disk");
		DataSourceTextField host_share_used_mem = new DataSourceTextField("host_share_used_mem", "host_share_used_mem");
		DataSourceTextField host_share_used_cpu = new DataSourceTextField("host_share_used_cpu", "host_share_used_cpu");
		DataSourceTextField host_share_running_vms = new DataSourceTextField("host_share_running_vms", "host_share_running_vms");

		DataSourceTextField template_cpuspeed = new DataSourceTextField("template_cpuspeed", "template_cpuspeed");
		DataSourceTextField template_freecpu = new DataSourceTextField("template_freecpu", "template_freecpu");
		DataSourceTextField template_freememory = new DataSourceTextField("template_freememory", "template_freememory");
		DataSourceTextField template_hostname = new DataSourceTextField("template_hostname", "template_hostname");
		DataSourceTextField template_hypervisor = new DataSourceTextField("template_hypervisor", "template_hypervisor");
		DataSourceTextField template_name = new DataSourceTextField("template_name", "template_name");
		DataSourceTextField template_totalcpu = new DataSourceTextField("template_totalcpu", "template_totalcpu");
		DataSourceTextField template_totalmemory = new DataSourceTextField("template_totalmemory", "template_totalmemory");
		DataSourceTextField template_usedcpu = new DataSourceTextField("template_usedcpu", "template_usedcpu");
		DataSourceTextField template_usedmemory = new DataSourceTextField("template_usedmemory", "template_usedmemory");

		setFields(idField, name, state, im_mad, vm_mad, tm_mad, last_mon_time, cluster, host_share_hid, host_share_disk_usage, host_share_mem_usage,
				host_share_cpu_usage, host_share_max_disk, host_share_max_mem, host_share_max_cpu, host_share_free_disk, host_share_free_mem,
				host_share_free_cpu, host_share_used_disk, host_share_used_mem, host_share_used_cpu, host_share_running_vms, template_cpuspeed,
				template_freecpu, template_freememory, template_hostname, template_hypervisor, template_name, template_totalcpu,
				template_totalmemory, template_usedcpu, template_usedmemory);

//		 setFields(host_share_hid, host_share_disk_usage,
//		 host_share_mem_usage, host_share_cpu_usage, host_share_max_disk,
//		 host_share_max_mem,
//		 host_share_max_cpu, host_share_free_disk, host_share_free_mem,
//		 host_share_free_cpu, host_share_used_disk, host_share_used_mem,
//		 host_share_used_cpu, host_share_running_vms);

		// setFields(template_cpuspeed, template_freecpu, template_freememory,
		// template_hostname, template_hypervisor, template_name,
		// template_totalcpu,
		// template_totalmemory, template_usedcpu, template_usedmemory);

		List<ListGridRecord> l = new ArrayList<ListGridRecord>();
		for (HostOca from : lists) {

			HostRecord to = new HostRecord();

			to.setId(from.getId());
			to.setName(from.getName());
			to.setState(from.getState());
			to.setIm_mad(from.getIm_mad());
			to.setVm_mad(from.getVm_mad());
			to.setTm_mad(from.getTm_mad());
			to.setLast_mon_time(from.getLast_mon_time());
			to.setCluster(from.getCluster());

			to.setHost_share_hid(from.getHost_share_hid());
			to.setHost_share_disk_usage(from.getHost_share_disk_usage());
			to.setHost_share_mem_usage(from.getHost_share_mem_usage());
			to.setHost_share_cpu_usage(from.getHost_share_cpu_usage());
			to.setHost_share_max_disk(from.getHost_share_max_disk());
			to.setHost_share_max_mem(from.getHost_share_max_mem());
			to.setHost_share_max_cpu(from.getHost_share_max_cpu());
			to.setHost_share_free_disk(from.getHost_share_free_disk());
			to.setHost_share_free_mem(from.getHost_share_free_mem());
			to.setHost_share_free_cpu(from.getHost_share_free_cpu());
			to.setHost_share_used_disk(from.getHost_share_used_disk());
			to.setHost_share_used_mem(from.getHost_share_used_mem());
			to.setHost_share_used_cpu(from.getHost_share_used_cpu());
			to.setHost_share_running_vms(from.getHost_share_running_vms());

			to.setTemplate_cpuspeed(from.getTemplate_cpuspeed());
			to.setTemplate_freecpu(from.getTemplate_freecpu());
			to.setTemplate_freememory(from.getTemplate_freememory());
			to.setTemplate_hostname(from.getTemplate_hostname());
			to.setTemplate_hypervisor(from.getTemplate_hypervisor());
			to.setTemplate_name(from.getTemplate_name());
			to.setTemplate_totalcpu(from.getTemplate_totalcpu());
			to.setTemplate_totalmemory(from.getTemplate_totalmemory());
			to.setTemplate_usedcpu(from.getTemplate_usedcpu());
			to.setTemplate_usedmemory(from.getTemplate_usedmemory());

			l.add(to);
		}

		setTestData(l.toArray(new HostRecord[0]));
		setClientOnly(true);
	}
}
